using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Fieldbook.Entities;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class FieldList : FieldbookPage
    {
        private List<Appuser> users = null;
        private List<Measureunit> measureUnits = null;
        private List<FieldPlantSpecie> fieldPlantSpecies = null;

        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            users = AppUserController.LoadHouseHoldUsers((long)Session["userID"]);
            measureUnits = MeasureUnitController.LoadMeasrueUnits(Measureunit.MeasureCategory.Area);
            fieldPlantSpecies = PlantSpecieController.LoadHouseHoldSpecies((long)Session["userID"]);

            ddlOwner.DataSource = users;
            ddlOwner.DataTextField="Firstname";
            ddlOwner.DataValueField = "Userid";
            ddlOwner.DataBind();

            ddlMeasureUnits.DataSource = measureUnits;
            ddlMeasureUnits.DataTextField="Description";
            ddlMeasureUnits.DataValueField = "Measureunitid";
            ddlMeasureUnits.DataBind();
        }

        protected void dgvFields_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                Label lblOwner = (Label)e.Row.Cells[1].FindControl("OwnerID");
                long userID = Int64.Parse(lblOwner.Text);
                Appuser user = users.Find(delegate(Appuser u) { return (long)u.Userid == userID; });
                lblOwner.Text = String.Format("{0} {1}", user.Firstname, user.Lastname);

                Label lblMeasure = (Label)e.Row.Cells[3].FindControl("lblMeasureUnit");
                int measureUnitID = Int32.Parse(lblMeasure.Text);
                Measureunit mUnit = measureUnits.Find(delegate(Measureunit mu) { return (int)mu.Measureunitid == measureUnitID; });
                lblMeasure.Text = mUnit.Description;

                Label lblPlantSpecie = (Label)e.Row.Cells[3].FindControl("lblFieldID");
                long fieldID = Int64.Parse(lblPlantSpecie.Text);
                FieldPlantSpecie fps = fieldPlantSpecies.Find(delegate(FieldPlantSpecie fPspecie) { return (long)fPspecie.FieldID == fieldID; });
                lblPlantSpecie.Text = (fps == null) ? "" : fps.Sort;
            }
        }

        protected void dgvFieldWorks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                Label lblExecutor = (Label)e.Row.Cells[0].FindControl("ExecutorID");
                long userID = Int64.Parse(lblExecutor.Text);
                Appuser user = users.Find(delegate(Appuser u) { return (long)u.Userid == userID; });
                lblExecutor.Text = String.Format("{0} {1}", user.Firstname, user.Lastname);
            }
        }

        /// <summary>
        /// Add new field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddField_Command(object sender, CommandEventArgs e)
        {
            Field f = new Field();
            f.Cataster = this.txtCataster.Text;
            f.Ownerid = Int64.Parse(this.ddlOwner.SelectedValue);
            f.Area = Decimal.Parse(this.txtArea.Text);
            f.Measureunitid = Int32.Parse(this.ddlMeasureUnits.SelectedValue);

            bool sucess = FieldController.InsertFiled(f);
            if (sucess)
            {
                lblOperationStatus.Text = "Field sucessfully added";
                this.txtCataster.Text = "";
                this.ddlOwner.SelectedIndex = -1;
                this.txtArea.Text = "";
                this.ddlMeasureUnits.SelectedIndex = -1;
                dgvFields.DataBind();
            }
            else
            {
                lblOperationStatus.Text = "Error adding field";
            }
        }
    }
}
